package Date;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @description:常用日期操作
 * @author: cyy
 **/
public class DateUtil {

    //获取到年月格式
    public String dateYearMonth(){
        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        String strDate1 = ldt.format(formatter);
        return strDate1;
    }

    //时间间隔分钟数  年月日 时分秒  str < 当前时间
    public  long dateSpaceMin(String str){
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        LocalDateTime localDateTimeFua = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));;
        Duration pe1 = Duration.between(localDateTimeFua.plusDays(1), localDateTimeNow);
        return  pe1.toMinutes();
    }

    //时间间隔 天  年月日 时分秒
    public  long dateSpaceDay(String str){
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        LocalDateTime localDateTimeFua = LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));;
        long until = localDateTimeFua.until(localDateTimeNow, ChronoUnit.DAYS);
        return  until;
    }

    //时间加减  天数
    //type   0+ 1-
    //int    +-
    public String dateAdd(int type ,int days){
        //当前日期加 +1 -1
        LocalDate localDate = LocalDate.now();
        String Days;
        if (type==0){
            Days = localDate.plusDays(days).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }else {
            Days = localDate.minusDays(days).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }
      return  Days;
    }

    //时间加减  分钟
    //type   0+ 1-
    //int    +-
    public String dateTimeAdd(int type ,int min){
        String format="";
        if (type==0){
            format = LocalDateTime.now().plusMinutes(min).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }else {
            format =LocalDateTime.now().plusMinutes(min).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }
        return  format;
    }
}
